<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>创建DOM例模式封装</title>
</head>
<body>
    <h1 id='creatdiv'>创建div例模式封装</h1>
    <h1 id='creatiframe'>创建iframe例模式封装</h1>
</body>
<script type="text/javascript">

    // 创建div
    var createWindow = function(){
        var div = document.createElement("div");
        div.innerHTML = "我是弹窗内容";
        div.style.display = 'none';
        document.body.appendChild(div);
        return div;
    };
    // 创建iframe
    var createIframe = function(){
        var iframe = document.createElement("iframe");
        iframe.style.width = '500px'
        iframe.style.height = '500px'
        document.body.appendChild(iframe);
        return iframe;
    };
    // 获取实例的封装代码
    var getInstance = function(fn) {
        var result;
        return function(){
            return result || (result = fn.call(this,arguments));
        }
    };
    // 测试创建div
    var createSingleDiv = getInstance(createWindow);
    document.getElementById("creatdiv").onclick = function(){
        var win = createSingleDiv();
        win.style.display = "block";
    };

    
    // 测试创建iframe
    var createSingleIframe = getInstance(createIframe);
    document.getElementById("creatiframe").onclick = function(){
        var win = createSingleIframe();
        win.src = "http://www.baidu.com";
    };

</script>
</html>